package com.water.wl.newwater;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.water.wl.newwater.widget.MyFragmentTabHost;


public class MainActivity extends FragmentActivity{

    public MyFragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        mTabHost = (MyFragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        initTabs();
    }

    private void initTabs(){
        MainTab []tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            ImageView image = (ImageView)indicator.findViewById(R.id.tab_image);
            //TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            /*title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);*/
            image.setImageDrawable(drawable);
            if (i == 2) {
                indicator.setVisibility(View.INVISIBLE);
                mTabHost.setNoTabChangedTag(getString(mainTab.getResName()));
            }
            //title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {

                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            mTabHost.addTab(tab, mainTab.getClz(), null);
            //mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
        }
    }


}

/*
package com.water.wl.newwater;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;



public class MainActivity extends Activity implements OnClickListener {
    private LinearLayout main_tweet;
    private LinearLayout main_explore;
    private LinearLayout main_camera;
    private LinearLayout main_news;
    private LinearLayout main_me;         //底部的按键

    private TweetTab tab0;
    private ExploreTab tab1;
    //private MainTabCamera tab2;
    private NewsTab tab3;
    private MeTab tab4;               //三个tab页面


    private FragmentManager fragmentManager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mainTab();
       */
/* Intent intent = new Intent(MainActivity.this,MyService.class);
        startService(intent);*//*


    }

    @Override
    protected void onRestart() {
        super.onRestart();
       // mainTab();
    }

    private void mainTab(){
        setContentView(R.layout.activity_main);
        initViewButton();
        fragmentManager = getFragmentManager();
        setTabSelection(0);

    }


    //初始化底部按钮，并设置监听
    private void initViewButton(){
        main_tweet = (LinearLayout)findViewById(R.id.main_tweet_linear);
        main_explore = (LinearLayout)findViewById(R.id.main_explore_linear);
        main_camera = (LinearLayout)findViewById(R.id.main_camera_linear);
        main_news = (LinearLayout)findViewById(R.id.main_news_linear);
        main_me = (LinearLayout)findViewById(R.id.main_me_linear);
        main_tweet.setOnClickListener(this);
        main_explore.setOnClickListener(this);
        main_camera.setOnClickListener(this);
        main_news.setOnClickListener(this);
        main_me.setOnClickListener(this);
    }



    //根据传入的参数选择tab
     private void setTabSelection(int i){
        resetButton();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //先隐藏掉所有fragment，以防止有多个fragment显示在界面上
        hideFragment(fragmentTransaction);
        //点击tab，改变控件的图片
        switch (i){
            case 0:
                ((ImageButton)main_tweet.findViewById(R.id.main_tweet_image))
                        .setImageResource(R.drawable.main_tweet_active);
             //如果messageFragment为空，则创建一个并添加到界面上
                if(tab0 == null){
                    tab0 = new TweetTab();
                    fragmentTransaction.add(R.id.id_content,tab0);
                }else {
                    fragmentTransaction.show(tab0);
                }
                break;
            case 1:
                ((ImageButton)main_explore.findViewById(R.id.main_explore_image))
                        .setImageResource(R.drawable.main_explore_active);
                if(tab1 == null){
                    tab1 = new ExploreTab();
                    fragmentTransaction.add(R.id.id_content,tab1);
                }else {
                    fragmentTransaction.show(tab1);
                }
                break;
            case 2:

               */
/* if(tab2 == null){

                    tab2 = new MainTabCamera();
                    fragmentTransaction.add(R.id.id_content,tab2);
                }else{

                    fragmentTransaction.show(tab2);
                }*//*

                break;
            case 3:
                ((ImageButton)main_news.findViewById(R.id.main_news_image))
                        .setImageResource(R.drawable.main_news_active);
                if(tab3 == null){

                    tab3 = new NewsTab();
                    fragmentTransaction.add(R.id.id_content,tab3);
                }else{

                    fragmentTransaction.show(tab3);
                }
                break;
            case 4:
                ((ImageButton)main_me.findViewById(R.id.main_me_image))
                        .setImageResource(R.drawable.main_me_active);
                if(tab4 == null){

                    tab4 = new MeTab();
                    fragmentTransaction.add(R.id.id_content,tab4);
                }else{

                    fragmentTransaction.show(tab4);
                }
                break;
            default:
                break;
        }
         fragmentTransaction.commit();
    }


    //清除所有选中状态，使按钮成为开始的绘图
    private void resetButton(){
        ((ImageButton)main_tweet.findViewById(R.id.main_tweet_image))
                .setImageResource(R.drawable.main_tweet);
        ((ImageButton)main_explore.findViewById(R.id.main_explore_image))
                .setImageResource(R.drawable.main_explore);
        ((ImageButton)main_camera.findViewById(R.id.main_camera_image))
                .setImageResource(R.drawable.main_camera);
        ((ImageButton)main_news.findViewById(R.id.main_news_image))
                .setImageResource(R.drawable.main_news);
        ((ImageButton)main_me.findViewById(R.id.main_me_image))
                .setImageResource(R.drawable.main_me);

    }

    //将所有fragment设置为隐藏状态
    private void hideFragment(FragmentTransaction fragmentTransaction){
        if(tab0 != null){
            fragmentTransaction.hide(tab0);
        }
        if(tab1 != null){
            fragmentTransaction.hide(tab1);
        }
        */
/*if(tab2 != null){
            fragmentTransaction.hide(tab2);
        }*//*

        if(tab3 != null){
            fragmentTransaction.hide(tab3);
        }
        if(tab4 != null){
            fragmentTransaction.hide(tab4);
        }
    }
    //点击进行选择
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.main_tweet_linear:
                setTabSelection(0);
                break;
            case R.id.main_explore_linear:
                setTabSelection(1);
                break;
            case R.id.main_camera_linear:
                ((ImageButton)main_camera.findViewById(R.id.main_camera_image))
                        .setImageResource(R.drawable.main_camera_down);
                //setTabSelection(2);
                quickCamera();
                break;
            case R.id.main_news_linear:
                setTabSelection(3);
                break;
            case R.id.main_me_linear:
                setTabSelection(4);
                break;
            default:
                break;
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        */
/*Intent intent = new Intent(MainActivity.this,MyService.class);
        stopService(intent);*//*

    }

    private void quickCamera(){}
}
*/
